package com.digua.base.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.ScreenUtils
import com.digua.base.R
import com.digua.base.databinding.DialogDefBinding
import com.digua.base.util.StrUtil.isNotBlank
import com.digua.base.util.ViewUtil

open class DefDialog : Dialog<DialogDefBinding>() {
    lateinit var title: String
    var message: String? = null
    var btn1Txt: String? = null
    var btn2Txt: String? = null
    var btn3Txt: String? = null
    var btn1ClickListener: View.OnClickListener? = null
    var btn2ClickListener: View.OnClickListener? = null
    var btn3ClickListener: View.OnClickListener? = null
    var btn1Hide = false
    var btn2Hide = true
    var btn3Hide = false
    var closeHide = false
    var contentView: View? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        var view = super.onCreateView(inflater, container, savedInstanceState)
        if (view == null) {
            ui = DialogDefBinding.inflate(layoutInflater)
            view = ui.getRoot()
        }
        title = getString(R.string.title)
        return view
    }

    override fun initUI() {
        super.initUI()
        ui.apply {
            val percentage = if (ScreenUtils.isLandscape()) 0.5F else 0.8F
            setWinWidth(percentage)
            message.visibility = if (contentView != null) {
                contentLayout.addView(contentView)
                View.GONE
            } else {
                View.VISIBLE
            }
            ViewUtil.onClickNoReClick(close) { _ -> dismiss() }
            btn1.setOnClickListener { btn1ClickListener?.onClick(it) ?: dismiss() }
            btn2.setOnClickListener { btn2ClickListener?.onClick(it) ?: dismiss() }
            btn3.setOnClickListener { btn3ClickListener?.onClick(it) ?: dismiss() }

            if (isNotBlank(this@DefDialog.title)) title.text = this@DefDialog.title
            if (isNotBlank(btn1Txt)) btn1.text = btn1Txt
            if (isNotBlank(btn2Txt)) btn2.text = btn2Txt
            if (isNotBlank(btn3Txt)) btn3.text = btn3Txt
            if (isNotBlank(this@DefDialog.message)) message.text = this@DefDialog.message
            close.visibility = if (closeHide) View.INVISIBLE else View.VISIBLE
            linex.visibility = View.VISIBLE
            btnLayout.visibility = View.VISIBLE
            if (!btn1Hide && !btn2Hide && !btn3Hide) {
                btn1.visibility = View.VISIBLE
                btn2.visibility = View.VISIBLE
                btn3.visibility = View.VISIBLE
            } else if (btn1Hide && btn2Hide && btn3Hide) {
                linex.visibility = View.GONE
                btnLayout.visibility = View.GONE
            } else {
                line1.visibility = if (btn1Hide || btn2Hide) View.GONE else View.VISIBLE
                line2.visibility = if ((btn2Hide || btn3Hide) && !(!btn1Hide && !btn3Hide)) View.GONE else View.VISIBLE
            }
            if (btn1Hide && btn2Hide && btn3Hide) {
                linex.visibility = View.GONE
                btnLayout.visibility = View.GONE
            } else {
                btn1.visibility = if (btn1Hide) View.GONE else View.VISIBLE
                btn2.visibility = if (btn2Hide) View.GONE else View.VISIBLE
                btn3.visibility = if (btn3Hide) View.GONE else View.VISIBLE
                var showNum = if (btn1Hide) 0 else 1
                showNum += if (btn2Hide) 0 else 1
                showNum += if (btn3Hide) 0 else 1
                if (showNum <= 1) {
                    line1.visibility = View.GONE
                    line2.visibility = View.GONE
                } else if (showNum == 3) {
                    line1.visibility = View.VISIBLE
                    line2.visibility = View.VISIBLE
                } else {
                    line1.visibility = if (btn1Hide) View.GONE else View.VISIBLE
                    line2.visibility = if (btn1Hide) View.VISIBLE else View.GONE
                }
            }
        }
    }

    /**
     * 设置Dialog宽度
     *
     * @param percentage 宽度占比（0~1之间）
     */
    public override fun setWinWidth(percentage: Float) {
        super.setWinWidth(percentage)
    }

}
